<?php
    // +----------------------------------------------------------------------
    // | Created by PHPstorm: JRKAdmin框架 [ JRKAdmin ]
    // +----------------------------------------------------------------------
    // | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
    // +----------------------------------------------------------------------
    // | SiteUrl: http://www.luckyhhy.cn
    // +----------------------------------------------------------------------
    // | Author: LuckyHhy <jackhhy520@qq.com>
    // +----------------------------------------------------------------------
    // | Date: 2020/1/16-15:20
    // +----------------------------------------------------------------------
    // | Description:  
    // +----------------------------------------------------------------------


    namespace app\wechat\controller;


    use think\Exception;
    use WeChat\Menu;
    use WeChat\User;

    class Test extends ChatBase
    {
        protected function initialize()
        {
            parent::initialize(); // TODO: Change the autogenerated stub
        }



        public function index(){
            try{

                $accessToken = $this->GetToken(); //获取token
                $config      = [
                    'appid'          => 'wx1c5c8f5cd32c11f',
                    'appsecret'      => '613d1459a988d7b54a82097f90de9e9',
                    'encodingaeskey' => '',
                    // 配置商户支付参数（可选，在使用支付功能时需要）
                    'mch_id'         => "",
                    'mch_key'        => '',
                    // 配置商户支付双向证书目录（可选，在使用退款|打款|红包时需要）
                    'ssl_key'        => '',
                    'ssl_cer'        => '',
                    // 缓存目录配置（可选，需拥有读写权限）
                    'cache_path'     => '',
                ];
               // $user_info   = new User($config);

               // $user_info->setAccessToken($accessToken['access_token']); //设置asstoken

                //$userlist=$user_info->getUserList();

              //  $menu=new Menu($config);
                //$menu->setAccessToken($accessToken['access_token']); //设置asstoken
                //dump($menu->get());

                //dump($user_info->getUserInfo("oTa7q5np76XDx5BTVdu47lorfW-w"));

                //dump($userlist);

            }catch (Exception $exception){
                dump($exception->getMessage());
            }



        }



        /**
         * @return mixed
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/1/16
         * @name: GetToken
         * @describe:获取本地缓存的accesstoken方法
         */
        public function GetToken()
        {
            $stoken = cache('S_TOKEN'); // 从缓存获取ACCESS_TOKEN
            if (is_array($stoken)) {
                $nowtime  = time();
                $difftime = $nowtime - $stoken['expires_time']; // 判读过期了没有；
                if ($difftime >= 0) { // 过期
                    $accesstoken = $this->getAccessToken(); // 去微信获取最新ACCESS_TOKEN
                    cache('S_TOKEN', $accesstoken); // 放进缓存
                }
            } else {
                $accesstoken = $this->getAccessToken(); // 去微信获取最新ACCESS_TOKEN
                cache('S_TOKEN', $accesstoken); // 放进缓存
            }

            return cache('S_TOKEN');
        }


        /**
         * @return bool|false|mixed|string
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/1/16
         * @name: getAccessToken
         * @describe:获取accesstoken方法
         */
        public function getAccessToken()
        {
            $url    = 'http://wxadmn.zsi.com.cn/p/x/inx';
            $result = file_get_contents($url);
            $result = json_decode($result, true);
            if ($result['status'] == 0) {
                return false;
            }

            return $result;
        }



    }